package com.dsm.alilinkvisualappdemo

import android.content.Context
import com.dwy.log.LogManager
import com.dwy.log.xlog.MyFlattener
import com.dwy.log.xlog.XLogImpl
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.libcat.LibCat
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.Printer
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.FileSizeBackupStrategy2
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.elvishew.xlog.printer.file.naming.FileNameGenerator
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone

object AppLogManager {

    private var logFilePath = ""

    fun init(context: Context) {
        logFilePath = context.filesDir.absolutePath + "/xlog/"
        //XLog.init(LogLevel.ALL)
        val config = LogConfiguration.Builder()
            .logLevel(
                /*if (BuildConfig.DEBUG) LogLevel.ALL // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
                else LogLevel.NONE*/
                LogLevel.ALL
            )
            .tag("plugincenter") // Specify TAG, default: "X-LOG"
            //.enableThreadInfo() // Enable thread info, disabled by default
            //.enableStackTrace(2) // Enable stack trace info with depth 2, disabled by default
            //.enableBorder() // Enable border, disabled by default
            /*.jsonFormatter(MyJsonFormatter()) // Default: DefaultJsonFormatter
            .xmlFormatter(MyXmlFormatter()) // Default: DefaultXmlFormatter
            .throwableFormatter(MyThrowableFormatter()) // Default: DefaultThrowableFormatter
            .threadFormatter(MyThreadFormatter()) // Default: DefaultThreadFormatter
            .stackTraceFormatter(MyStackTraceFormatter()) // Default: DefaultStackTraceFormatter
            .borderFormatter(MyBoardFormatter()) // Default: DefaultBorderFormatter
            .addObjectFormatter(
                AnyClass::class.java,  // Add formatter for specific class of object
                AnyClassObjectFormatter()
            ) // Use Object.toString() by default
            .addInterceptor(
                BlacklistTagsFilterInterceptor( // Add blacklist tags filter
                    "blacklist1", "blacklist2", "blacklist3"
                )
            )
            .addInterceptor(MyInterceptor())*/ // Add other log interceptor
            .build()

        val androidPrinter: Printer = AndroidPrinter(false) // Printer that print the log using android.util.Log

        //val consolePrinter: Printer = ConsolePrinter() // Printer that print the log to console using System.out

        val filePrinter =
            FilePrinter.Builder(logFilePath) // Specify the directory path of log file(s)
                .fileNameGenerator(object : FileNameGenerator {
                    override fun isFileNameChangeable(): Boolean {
                        return true
                    }

                    override fun generateFileName(logLevel: Int, timestamp: Long): String {
                        return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).apply {
                            timeZone = TimeZone.getDefault()
                        }.format(Date(timestamp)) + ".txt"
                    }
                }) // Default: ChangelessFileNameGenerator("log")
                .backupStrategy(FileSizeBackupStrategy2(8 * 1024 * 1024, 10)) // Default: FileSizeBackupStrategy(1024 * 1024)
                .cleanStrategy(FileLastModifiedCleanStrategy(2 * 24 * 60 * 60 * 1000L)) // Default: NeverCleanStrategy()
                .flattener(MyFlattener()) // Default: DefaultFlattener
//                .writer(MyWriter()) // Default: SimpleWriter
                .build()

        XLog.init( // Initialize XLog
            config,  // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
            androidPrinter,  // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
            /*consolePrinter,*/
            filePrinter
        )
        LibCat.config(true, filePrinter)

        LogManager.iLog = XLogImpl().apply {
            this.filePrinter = filePrinter
            consoleLogSwitch = BuildConfig.DEBUG
            fileLogSwitch = true
        }
        val logObjectType = LogObjectType(
            LogObject(
                arrayListOf(
                    "Coap",
                    "libjingle",
                    "LK-core-",
                    "APITracker",
                    "linksdk",
                    "IoTCredential",
                    "OALoginAdapter",
                    "PersistentConnect",
                    "ARouter",
                    "OSS-Android-SDK",
                    "GlobalConfig",
                    "LV"
                ),
                LogManager.BaseLoger("Alibaba")
            ),
            LogObject(
                arrayListOf("FFmpegCmd"),
                LogManager.BaseLoger("System")
            )
        )
        val filterTags = arrayListOf<String>().apply {
            addAll(logObjectType.alibaba.filterTags)
            addAll(logObjectType.system.filterTags)
        }
        LogManager.startLogListener(filterTags) { tag: String, shotMsg: String ->
            if (logObjectType.alibaba.filterTags.contains(tag)) {
                logObjectType.alibaba.baseLoger.writeFile(shotMsg)
            } else if (logObjectType.system.filterTags.contains(tag)) {
                logObjectType.system.baseLoger.writeFile(shotMsg)
            }
        }
    }

    data class LogObject (val filterTags: ArrayList<String>, val baseLoger: LogManager.BaseLoger)
    data class LogObjectType(val alibaba: LogObject, val system: LogObject)
}